{% include "GdsVenteBundle::BonLivraison/bon_to_fac_script.html.twig" %}
<div class="span12">
    <div class="box gradient">
        <div class="title" style="text-align: center">
            <h4>Facturation de  {{bonlivraison}}</h4>
        </div>
        <div class="content noPad clearfix">
            <table style="width: 100%">
                <tr>
                    <td style="width: 20%">
                        <label>Date:</label>
                        <div>
                            <input id="date" type="text" />
                            <input id="now" type="text" style="width: 70px;" readonly/>
                        </div>
                    </td>
                    <td  style="width: 20%">
                        <label>Client:</label>
                                    {{bonlivraison.client}}
                    </td>
                    <td  style="width: 20%">
                        <label>Departement:</label>
                            {{bonlivraison.departement}}
                    </td>
                    <td  style="width: 40%">
                        <label>Numero Facture:</label>
                                {{ reference}}
                    </td>
                </tr>
            </table>

        </div> 
    </div>
</div>
<div class="span12" style="margin-left:0">
    <div class="box gradient">
        <div class="content noPad clearfix">
            <table id="table_facture" cellpadding="0" cellspacing="0" border="0" class="responsive  display table table-bordered" width="100%">
                <thead>
                    <tr >
                        <th>N°</th>
                        <th>Reference</th>
                        <th>Designation</th>
                        <th>Qte</th>
                        <th>P.U.HT</th>
                        <th>TVA</th>
                        <th>Remise</th>
                        <th>P.U.TTC</th>
                        <th id="th_total" style="width:10%">Total</th>
                    </tr>
                </thead>
                <tbody id="lignes_bon_livraison_to_facture">
                </tbody>
            </table>
        </div>
    </div>
    <div class="span2" style="margin-left:83%">
        <div class="content noPad clearfix">
            <table  border="1px" class="responsive  display table table-bordered" width="100%" style="border:#D0E9F5 ">
                <thead>
                    <tr style="width: 100%">
                        <th style="text-align: center;width: 50%">Montant Total</th>
                        <th id="montant_total"  style="width: 50%">0.000</th>
                    </tr>
                    <tr>
                        <th style="text-align: center">Timbre</th>
                        <th id="timbre" >{{timbre|number_format(3)}}</th>
                    </tr>
                    <tr>
                        <th style="text-align: center">Total TTC</th>
                        <th id="montant_total_ttc" >{{timbre|number_format(3)}}</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>
    {# Ajout d'un lien pour ajouter un champ cat�gorie suppl�mentaire. #}
<div class="span2" style="margin-left:0">
    <div class="box gradient">
        <div class="content noPad clearfix">
            <table id="taxe" cellpadding="0" cellspacing="0" border="0" class="responsive  display table table-bordered" width="100%">
                <thead>
                    <tr>
                        <th>Taxe</th>
                        <th>Base</th>
                        <th>Montant</th>
                    </tr>
                </thead>
                <tbody id="taxe_body">
                    <tr id="tr_taxe_18">
                        <td id="taxe_18">18%</td>
                        <td id="base_18">0.000</td>
                        <td id="montant_taxe_18">0.000</td>
                    </tr>
                    <tr>
                        <td>Totaux</td>
                        <td></td>
                        <td id="total_taxe">0.000</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="span2" style="margin-right: 0">
    <table>
        <tr>
            <td style="text-align: center">
                <input type="button" style="width:100px" id="submit_form_facture" value="Enregistrer" class="nostyle btn btn-success btn-large" />
            </td>
            <td style="text-align: center">
                <input type="button" style="width:100px" id="submit_form_facture_print" value="Imprimer" class="nostyle btn btn-success btn-large" />
            </td>
        </tr>
        <tr>
            <td style="height: 20px"></td>
        </tr>
        <tr>
            <td>
                <input id="retour2" style="width:100px" type="button" class="btn btn-info cut-icon-previous"  value="Retour" onclick="retourListeBonLivraison2()">
            </td>
        </tr>
    </table>
</div>
<script>
    $(document).ready(function() {
        $('#submit_form_facture').click(function() {
            $('#submit_form_facture').hide()
            $('#submit_form_facture_print').hide()
            $.ajax({
                url: '{{path('gds_vente_bon_livraison_facturer')}}',
                type: "POST",
                datatype: 'json',
                data: 'id=' + '{{id}}' + '&date=' + $('#date').val() + '&heure=' + $('#heure').val(),
                success: function(data) {
                    if (data.success) {
                        $.msgBox({
                            title: "Facture",
                            content: "Enregistrement Effectué avec succés",
                            type: "info",
                            buttons: [{value: "OK"}],
                            success: function(result) {
                                location.reload();
                            }
                        });
                    } else {
                        $.msgBox({
                            title: "Facture",
                            content: "Erreur D'enregistrement",
                            type: "error",
                            buttons: [{value: "OK"}],
                            success: function(result) {
                                $('#submit_form_facture').show("slow")
                                $('#submit_form_facture_print').show("slow")
                            }
                        });
                    }
                },
                error: function(e) {
                    $.msgBox({
                        title: "Facture",
                        content: "Erreur D'enregistrement",
                        type: "error",
                        buttons: [{value: "OK"}],
                        success: function(result) {
                            $('#submit_form_facture').show("slow")
                            $('#submit_form_facture_print').show("slow")
                        }
                    });
                }
            })
        })
        $('#submit_form_facture_print').click(function() {
            $('#submit_form_facture').hide()
            $('#submit_form_facture_print').hide()
            $.ajax({
                url: '{{path('gds_vente_bon_livraison_facturer')}}',
                type: "POST",
                datatype: 'json',
                data: 'id=' + '{{id}}' + '&date=' + $('#date').val() + '&heure=' + $('#heure').val(),
                success: function(data) {
                    if (data.success) {
                        Imprimer(data.id)
                    } else {
                        $.msgBox({
                            title: "Facture",
                            content: "Erreur D'enregistrement",
                            type: "error",
                            buttons: [{value: "OK"}],
                            success: function(result) {
                                $('#submit_form_facture').show("slow")
                                $('#submit_form_facture_print').show("slow")
                            }
                        });
                    }
                },
                error: function(e) {
                    $.msgBox({
                        title: "Facture",
                        content: "Erreur D'enregistrement",
                        type: "error",
                        buttons: [{value: "OK"}],
                        success: function(result) {
                            $('#submit_form_facture').show("slow")
                            $('#submit_form_facture_print').show("slow")
                        }
                    });
                }
            })
        })
        $('#date').datepicker();
        var fullDate = new Date();
        var twoDigitMonth = fullDate.getMonth()
        twoDigitMonth++
        twoDigitMonth += "";
        if (twoDigitMonth.length == 1)
            twoDigitMonth = "0" + twoDigitMonth;
        var twoDigitDate = fullDate.getDate() + "";
        if (twoDigitDate.length == 1)
            twoDigitDate = "0" + twoDigitDate;
        var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
        $("#date").val(currentDate)
        setInterval(time, 1000);
        index = 0;
        nb_article = 1;
        $container = $('#lignes_bon_livraison_to_facture');
      {% for l,ligne in lignes %}
        add_ligne_from_bl_blanc()
        var id = index - 1
        $('#td_reference_' + id).html('{{ligne.article.reference}}')
        $('#td_designation_' + id).html('{{ligne.article.designation}}')
        $('#td_qte_' + id).html('{{ligne.quantiteBlanc|number_format(2)}}')
        $('#td_prix_unitaire_ht_' + id).html('{{ligne.prixUnitaireHt}}')
        var tva_twig = {{ligne.tauxTva}}
        tva_twig = tva_twig.toFixed(2)
        $('#td_tva_' + id).html(tva_twig + '%')
        $('#td_remise_' + id).html('{{ligne.tauxRemise}}')
        $('#gds_ventebundle_' + id + '_article').val('{{ligne.article.id}}')
        var prix_ht = {{ligne.prixUnitaireHt}}
        var remise = {{ligne.tauxRemise}}
        var prix_uttc = prix_ht * (1 + tva_twig / 100) * (1 - remise / 100)
        prix_uttc = prix_uttc.toFixed(3)
        var quantite = {{ligne.quantiteBlanc}}
        var total = quantite * prix_uttc
        $('#td_prix_u_ttc_' + id).html(prix_uttc)
        $('#td_total_' + id).html(total.toFixed(3))
     {% endfor %}
        getTaxeInitial()
    })
    function add_ligne_from_bl_blanc() {
        var td_num = '<td class="numero_ligne" style="padding:0;text-align:center" id="td_num_' + index + '">' + nb_article + '</td>';
        var hidden_stock = '<input id="gds_ventebundle_' + index + '_article" type="hidden"/>';
        var td_reference = '<td style="padding:0;width:10%" id="td_reference_' + index + '"></td>';
        var td_designation = '<td style="padding:0;width:30%" id="td_designation_' + index + '"></td>';
        var td_qte = '<td style="padding:0;width:10%" id="td_qte_' + index + '"></td>';
        var td_prix_unitaire_ht = '<td style="padding:0" id="td_prix_unitaire_ht_' + index + '"></td>';
        var td_tva = '<td style="padding:0" id="td_tva_' + index + '">18.00%</td>';
        var td_remise = '<td style="padding:0;width:10%" id="td_remise_' + index + '"></td>';
        var td_prix_u_ttc = '<td style="padding:0" id="td_prix_u_ttc_' + index + '"></td>';
        var td_total = '<td style="padding:0;width:10%" id="td_total_' + index + '">0.000</td>';
        var tr_contenu = td_num + td_reference + td_designation + hidden_stock + td_qte + td_prix_unitaire_ht + td_tva + td_remise + td_prix_u_ttc + td_total;
        var tr = $('<tr style="height:30px" id="tr_' + index + '">' + tr_contenu + '</tr>')
        $container.append(tr);
        index++
        nb_article++
    }
    function getTaxeInitial() {
        var rowCount = $('#lignes_bon_livraison_to_facture tr').length
        var total_ttc = 0
        var taux_tva = new Array();
        var taxes = new Array();
        for (var i = 0; i < rowCount; i++) {
            taxes[i] = new Array()
            taxes[i]['tva'] = 0
            taxes[i]['base'] = 0
            taxes[i]['montant'] = 0
        }
        var cmp = 0
        var i_refraich = 0
        while (cmp < rowCount) {
            if ($('#td_total_' + i_refraich).html() != null) {
                var tva = parseFloat($('#td_tva_' + i_refraich).html())
                var j = $.inArray(tva, taux_tva)
                if (j == -1) {
                    taux_tva.push(tva)
                    j = $.inArray(tva, taux_tva)
                }
                var ttc = parseFloat($('#td_total_' + i_refraich).html())
                total_ttc += ttc
                var ht = ttc / (1 + tva / 100)
                var montant = ht * tva / 100
                taxes[j]['tva'] = tva
                taxes[j]['base'] += ht
                taxes[j]['montant'] += montant
                cmp++
            }
            i_refraich++
        }
        taxes = TriTab(taux_tva, taxes)
        $('#taxe_body tr').remove()
        var totaux = 0
        var count_taux_tva = taux_tva.length
        for (var i = 0; i < count_taux_tva; i++) {
            tva = taxes[i]['tva'].toFixed(2)
            totaux += taxes[i]['montant']
            $('#taxe_body').append('<tr id="tr_taxe_' + tva + '"><td id="taxe_' + tva + '">' + tva + '%</td><td id="base_' + tva + '">' + taxes[i]['base'].toFixed(3) + '</td><td id="montant_taxe_' + tva + '">' + taxes[i]['montant'].toFixed(3) + '</td></tr>');
        }
        $('#taxe_body').append('<tr><td>Totaux</td><td></td><td id="total_taxe">' + totaux.toFixed(3) + '</td></tr>');
        $('#montant_total').html(total_ttc.toFixed(3))
        total_ttc += {{timbre}}
        $('#montant_total_ttc').html(total_ttc.toFixed(3))
    }
    function retourListeBonLivraison2() {
        $.ajax({
            url: "{{ path('gds_vente_liste_bon_livraison') }}",
            data: 'retour=true' + '&numero=' + lf[0] + '&client=' + lf[1] + '&date1=' + lf[2] + '&date2=' + lf[3] + '&heure1=' + lf[4] + '&heure2=' + lf[5]
                    + '&reference=' + lf[6] + '&designation=' + lf[7] + '&ttc1=' + lf[8] + '&ttc2=' + lf[9] + '&is_facturer=' + lf[10]
                    + '&reglement=' + lf[11] + '&departement=' + lf[12],
            success: function(data) {
                $('#div_liste_bon_livraison').replaceWith(data)
                $('#filtre_numero').val(lf[0])
                $('#client').val(lf[1])
                $('#filtre_date1').val(lf[2])
                $('#filtre_date2').val(lf[3])
                $('#filtre_reference').val(lf[6])
                $('#filtre_designation').val(lf[7])
                $('#filtre_ttc1').val(lf[8])
                $('#filtre_ttc2').val(lf[9])
                $('#filtre_type').val(lf[10])
                $('#filtre_reglement').val(lf[11])
                $('#departement').val(lf[12])
            }
        })
    }
</script>
<input type="hidden" id="heure"/>